<?php

$uid = $_G['uid'];
$user = getuserbyuid($uid);
if(empty($user)) {
    showmessage('用户信息获取失败', '', array(), array('showdialog' => true, 'locationtime' => 3));
    exit;
}

$table_er_kami_buy_config = C::t('#erling_kami_buy#er_kami_buy_config');
$table_er_kami_buy = C::t('#erling_kami_buy#er_kami_buy');
$table_er_kami_buy_type = C::t('#erling_kami_buy#er_kami_buy_type');
$table_er_kami_buy_type_list = ($table_er_kami_buy_type->fetch_all())['data'];


$er_kami_buy_config = $table_er_kami_buy_config->fetch_by_keys([
    'allowed_groups','precautions', 'nav_name', 'recharge_url'
]);
$er_kami_buy_config['recharge_url']['value']??= 'home.php?mod=spacecp&ac=credit&op=buy';
$allowed_groups = dunserialize($er_kami_buy_config['allowed_groups']['value']);
// 获取用户所有用户组(主用户组+扩展用户组)
$user_groups = array($user['groupid']);
if(!empty($user['extgroupids'])) {
    $extgroups = explode("\t", trim($user['extgroupids'], "\t"));
    $user_groups = array_merge($user_groups, $extgroups);
}
$has_permission = false;
foreach($user_groups as $group) {
    if(in_array($group, $allowed_groups)) {
        $has_permission = true;
        break;
    }
}

if(!$has_permission) {
    showmessage('您的用户组无法访问', '', array(), array('showdialog' => true, 'locationtime' => 3));
    exit;
}

// 积分类型
$extcredits_array = DB::fetch_all("SELECT * FROM %t WHERE skey LIKE %s", array('common_setting', 'extcredits%'));
if ($extcredits_array && isset($extcredits_array[0]['svalue'])) {
    $extcredits_data = unserialize($extcredits_array[0]['svalue']);
} else {
    $extcredits_data = [];
}


if($_GET['buy_kami']) {
    if(!submitcheck('formhash')) {
        $data = [
            'success' => false,
            'message' => '请求来源验证失败'
        ];
        echo json_encode($data);
        exit;
    }
    $kamiTypeId = $_POST['kami_type'];
    $kami_buy_type = $table_er_kami_buy_type->fetch($kamiTypeId);
    
    // 验证用户积分余额是否足够扣除
    $userCredit = getuserprofile('extcredits'.$kami_buy_type['extcredits'], $uid);
    if($userCredit < $kami_buy_type['credit']) {
        $data = [
            'success' => false,
            'message' => '您的'.$extcredits_data[$kami_buy_type['extcredits']]['title'].'不足，需要'.$kami_buy_type['credit']
        ];
        echo json_encode($data);
        exit;
    }
    

    // 获取卡密
    $kami = $table_er_kami_buy->buy_kami_by_type_id($kamiTypeId, $uid, $kami_buy_type['extcredits'], $kami_buy_type['credit']);
    if(!$kami) {
        $data = [
            'success' => false,
            'message' => '获取失败或无剩余卡密'
        ];
    } else {
        $data = [
            'success' => true,
            'message' => '获取成功',
            'kami' => $kami,
            'kami_buy_type' => $kami_buy_type['name'],
        ];
        // 积分扣除
        updatemembercount($uid, $dataarr = array($kami_buy_type['extcredits'] => -$kami_buy_type['credit']), $checkgroup = true, $operation = '', $relatedid = 0, $ruletxt = '');
        
    }
    echo json_encode($data);
    exit;
}

// 获取当前用户所有积分
$credits = DB::fetch_first("SELECT * FROM %t WHERE uid=%d", array('common_member_count', $uid));
$credits = [
    'extcredits1' => [
        'value' => $credits['extcredits1'],
        'title' => $extcredits_data[1]['title']
    ],
    'extcredits2' => [
        'value' => $credits['extcredits2'],
        'title' => $extcredits_data[2]['title']
    ],
    'extcredits3' => [
        'value' => $credits['extcredits3'],
        'title' => $extcredits_data[3]['title']
    ],
    'extcredits4' => [
        'value' => $credits['extcredits4'],
        'title' => $extcredits_data[4]['title']
    ],
    'extcredits5' => [
        'value' => $credits['extcredits5'],
        'title' => $extcredits_data[5]['title']
    ],
    'extcredits6' => [
        'value' => $credits['extcredits6'],
        'title' => $extcredits_data[6]['title']
    ],
    'extcredits7' => [
        'value' => $credits['extcredits7'],
        'title' => $extcredits_data[7]['title']
    ],
    'extcredits8' => [
        'value' => $credits['extcredits8'],
        'title' => $extcredits_data[8]['title']
    ]
];

include template('erling_kami_buy:user_kami_exchange');